package com.molyfun.parents.activity.store.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.CommitCommentActivity;
import com.molyfun.parents.adapter.ProductCommentListAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.ProductCommentListResponse;
import com.molyfun.parents.rest.bean.ProductDetailInfoResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProductDetailEvaluateFragment extends BaseFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    @InjectView(R.id.evaluate_btn)
    ImageView mEvaluateBtn;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.simpleRatingBar)
    ScaleRatingBar mScaleRatingBar;
    @InjectView(R.id.score)
    TextView mScore;
    @InjectView(R.id.evaluate_count)
    TextView mEvaluateCount;


    private ProductDetailInfoResponse.ProductDetailInfoBean mProductDetailBean;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    private ProductCommentListAdapter mProductCommentListAdapter;
    private List<ProductCommentListResponse.ProductCommentBean> mCommentList;

    LinearLayoutManager mLayoutManager;

    public static ProductDetailEvaluateFragment newInstance(ProductDetailInfoResponse.ProductDetailInfoBean productDetailBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Const.PRODUCT_DETAIL_BEAN, productDetailBean);
        ProductDetailEvaluateFragment productDetailInfoFragment = new ProductDetailEvaluateFragment();
        productDetailInfoFragment.setArguments(bundle);
        return productDetailInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_detail_evaluate, container, false);
        ButterKnife.inject(this, view);

        mProductDetailBean = getArguments().getParcelable(Const.PRODUCT_DETAIL_BEAN);
        mProductCommentListAdapter = new ProductCommentListAdapter(getContext());
//        if (mProductDetailBean != null) {
//            getData(true);
//            mScaleRatingBar.setRating(Float.parseFloat(mProductDetailBean.getDegree()));
//        }

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);
        mRecyclerView.setAdapter(mProductCommentListAdapter);


        if(mProductDetailBean != null){
            if(mProductDetailBean.isPurchasestatus()){
                mEvaluateBtn.setVisibility(View.VISIBLE);
            }else{
                mEvaluateBtn.setVisibility(View.GONE);
            }
        }

        mEvaluateBtn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.evaluate_btn:
                CommitCommentActivity.startActivity(getContext(), mProductDetailBean.getId());
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mProductDetailBean != null) {
            getData(true);
            if(TextUtils.isEmpty(mProductDetailBean.getDegree())){
                mScaleRatingBar.setRating(0f);
            }else{
                mScaleRatingBar.setRating(Float.parseFloat(mProductDetailBean.getDegree()));
            }
        }
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            getData(false);
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getData(true);
    }

    private void getData(final boolean isRefresh) {
        showProgressDialog();
        Call<ProductCommentListResponse> call = MFPApplication.getInstance().getRestClient().getProductCommentList(mProductDetailBean.getId(), mCurrentPage, RestClient.DEFAULT_PAGE_SIZE);
        call.enqueue(new Callback<ProductCommentListResponse>() {
            @Override
            public void onResponse(Call<ProductCommentListResponse> call, Response<ProductCommentListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200 && response.body().getData() != null) {

                        if(TextUtils.isEmpty(response.body().getData().getSubjectdegree())){
                            mScore.setText("0分");
                        }else{
                            mScore.setText(response.body().getData().getSubjectdegree() + "分");
                            mScaleRatingBar.setRating(Float.parseFloat(response.body().getData().getSubjectdegree()));
                        }

                        mEvaluateCount.setText(response.body().getData().getCommenttimes() + "人评价");

                        if(isRefresh){
                            if(response.body().getData().getCommentlist() != null && response.body().getData().getCommentlist().size() > 0){
                                mCommentList = response.body().getData().getCommentlist();
                                mProductCommentListAdapter.refreshData(mCommentList);
                                mHasMoreData = true;
                                mEmptyLayout.setVisibility(View.GONE);
                            }else{
                                mHasMoreData = false;
                                mProductCommentListAdapter.clearAll();
                                mEmptyLayout.setVisibility(View.VISIBLE);
                            }
                        }else{
                            if(response.body().getData().getCommentlist().size() > 0){
                                mHasMoreData = true;
                            }else{
                                mHasMoreData = false;
                            }
                            mProductCommentListAdapter.addAll(response.body().getData().getCommentlist());
                        }




                    } else {
                        toast(response.body().getMsg());
                    }
                } else {
                    toast("获取产品详情失败");
                }
            }

            @Override
            public void onFailure(Call<ProductCommentListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
